package com.meteor.user.controller;

import com.meteor.framework.common.base.ResponseData;
import com.meteor.framework.common.utils.PageResult;
import com.meteor.framework.operatelog.annotations.OperateLog;
import com.meteor.framework.operatelog.enums.OperateTypeEnum;
import com.meteor.user.convert.SysMerchantConvert;
import com.meteor.user.convert.SysOrgConvert;
import com.meteor.user.entity.SysMerchant;
import com.meteor.user.entity.SysOrg;
import com.meteor.user.query.SysMerchantQuery;
import com.meteor.user.query.SysRoleQuery;
import com.meteor.user.service.SysMerchantService;
import com.meteor.user.service.SysOrgService;
import com.meteor.user.vo.SysMerchantVO;
import com.meteor.user.vo.SysOrgVO;
import com.meteor.user.vo.SysRoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商户管理
 *
 * @author lynn
 * @date 2024/1/03
 */
@RestController
@RequestMapping("sys/merchant")
@Tag(name = "商户管理")
@AllArgsConstructor
public class SysMerchantController {
    private final SysMerchantService sysMerchantService;

    @GetMapping("page")
    @Operation(summary = "分页查询")
    @PreAuthorize("hasAuthority('sys:merchant:page')")
    public ResponseData page(@ParameterObject @Valid SysMerchantQuery query) {

        PageResult<SysMerchantVO> page = sysMerchantService.page(query);

        return ResponseData.success(page);
    }

    @GetMapping("allList")
    @Operation(summary = "全部查询")
    @PreAuthorize("hasAuthority('sys:merchant:all')")
    public ResponseData allList() {

        List<SysMerchantVO> page = sysMerchantService.allList();

        return ResponseData.success(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('sys:merchant:info')")
    public ResponseData get(@PathVariable("id") String id) {
        return ResponseData.success("查询成功",SysMerchantConvert.INSTANCE.convert(sysMerchantService.getById(id)));
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('sys:merchant:save')")
    public ResponseData save(@RequestBody @Valid SysMerchantVO vo) {
        sysMerchantService.save(vo);

        return ResponseData.success("新增成功");
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('sys:merchant:update')")
    public ResponseData update(@RequestBody @Valid SysMerchantVO vo) {
        sysMerchantService.update(vo);

        return ResponseData.success("修改成功");
    }

    @DeleteMapping("{id}")
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('sys:merchant:delete')")
    public ResponseData delete(@PathVariable("id") String id) {
        sysMerchantService.delete(id);

        return ResponseData.success("删除成功");
    }

    @GetMapping("/orgMerchant/{orgId}")
    @Operation(summary = "根据组织id查询商户")
    @PreAuthorize("hasAuthority('sys:merchant:list')")
    public ResponseData orgMerchant(@PathVariable("orgId") String orgId) {

        SysMerchantVO sysMerchantVO = sysMerchantService.orgMerchant(orgId);

        return ResponseData.success(sysMerchantVO);
    }
}